﻿using INTEX.DataBase;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WPFExcelReport.ViweModels;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SETUPABON.xaml
    /// </summary>
    public partial class ITXGABBS : UserControl
    {
        ITXGABBSViewModel vm;
        public ITXGABBS()
        {
            InitializeComponent();
            vm = new ITXGABBSViewModel();
            this.DataContext = vm;
        }
        #region=========Property========= 
        public static readonly DependencyProperty SetMasterProperty =
DependencyProperty.Register("SetMaster", typeof(SETINTEXDB), typeof(ITXGABBS));
        public SETINTEXDB SetMaster
        {
            get
            {
                return this.GetValue(SetMasterProperty) as SETINTEXDB;
            }
            set
            {
                this.SetValue(SetMasterProperty, value);
            }
        }
        public static readonly DependencyProperty SetDetailProperty =
DependencyProperty.Register("SetDetail", typeof(SETINTEXDB), typeof(ITXGABBS));
        public SETINTEXDB SetDetail
        {
            get
            {
                return this.GetValue(SetDetailProperty) as SETINTEXDB;
            }
            set
            {
                this.SetValue(SetDetailProperty, value);
            }
        }
        public static readonly DependencyProperty SetEnvProperty =
DependencyProperty.Register("SetEnv", typeof(SETINTEXDB), typeof(ITXGABBS));
        public SETINTEXDB SetEnv
        {
            get
            {
                return this.GetValue(SetEnvProperty) as SETINTEXDB;
            }
            set
            {
                this.SetValue(SetEnvProperty, value);
            }
        }
      
        #region=====vwTMPGABBS=======
        public CollectionViewSource vwTMPGABBS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPGABBS");
            }
        }
        public DataRow dwTMPGABBS
        {
            get
            {
                if (vwTMPGABBS.View == null)
                    return null;
                if ((DataRowView)vwTMPGABBS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPGABBS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwGABBS=======
        public CollectionViewSource vwGABBS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwGABBS");
            }
        }
        public DataRow dwGABBS
        {
            get
            {
                if (vwGABBS.View == null)
                    return null;
                if ((DataRowView)vwGABBS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwGABBS.View.CurrentItem).Row;
            }
        }
        #endregion
        private Dictionary<string, object> dicParamater
        {
            get
            {
                return vm.dicParamater;
            }
            set
            {
                vm.dicParamater = value;
            }
        }

        public string Mandant
        {
            get
            {
                return vm.Mandant;
            }
        }
        public string company
        {
            get
            {
                return vm.company;
            }
        }
        #endregion
        private void New_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwGABBS == null)
                    return;
                DataRow dwNew = SetDetail.ICC_GABBS.NewRow();
                dwNew.ItemArray = dwGABBS.ItemArray;
                dwNew["ROWNID"] = "0";
                SetDetail.ICC_GABBS.Rows.Add(dwNew);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Load_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = vm.Load(vm.Mandant);
                SetMaster.Clear();
                SetMaster.Merge(setResult, false, MissingSchemaAction.Ignore);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Copy_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwGABBS == null)
                    return;
                DataRow dwNew = SetDetail.ICC_GABBS.NewRow();
                dwNew.ItemArray = dwGABBS.ItemArray;
                dwNew["ROWNID"] = "0";
                SetDetail.ICC_GABBS.Rows.Add(dwNew);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetDetail.Clear();
                SetDetail.Merge(SetMaster.ICC_GABBS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            finally
            {

            }
        }
        private void CopyAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetDetail.Clear();
                SetDetail.Merge(SetMaster.ICC_GABBS);
                foreach (DataRow RX in SetDetail.ICC_GABBS.Rows)
                {
                    RX["ROWNID"] = "0";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }

        }
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                vm.Save(SetDetail, "00");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butDiscard_Click(object sender, RoutedEventArgs e)
        {
            SetDetail.Clear();
            SetDetail.AcceptChanges();
        }
    }
}
